home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / VD08SMP.ZIP / usr / samples / dbview / Controller.m < prev    next >
Encoding:
Text File  |  1996-02-13  |  6.0 KB  |  213 lines

  1. /* -------------------------------------------------------------------
  2.  
  3.     Project: 
  4.  
  5.     Objective-C source file for the class Controller
  6.  
  7.     This file was automatically generated by OS/2 Project Builder
  8.     COPYRIGHT (C), 1995, Thomas Baier
  9.  
  10.     Unregistered version
  11.  
  12.     COPYRIGHT (C), 1995
  13.     ALL RIGHTS RESERVED.
  14.  
  15.     Date:                           Rev:
  16.     Sat Oct 14 02:07:57 1995
  17.  
  18.  
  19. ------------------------------------------------------------------- */
  20.  
  21. /* ------------------------ Include files ------------------------- */
  22. #include <pm/pm.h>
  23. #include <db/db.h>
  24. #include <stdlib.h>
  25.  
  26. /* ------------------------  Classes used  ------------------------ */
  27.  
  28.  
  29. /* -------------------------  Externals  -------------------------- */
  30.  
  31.  
  32. /* --------------------------  Defines  --------------------------- */
  33.  
  34.  
  35. /* ----------------------  Class variables  ----------------------- */
  36.  
  37.  
  38. /*====================================================================
  39.                      Implementation of class Controller
  40. ====================================================================*/
  41. #include "Controller.h"
  42.  
  43. @implementation Controller : Object
  44. {
  45.   id button;
  46.   id container;
  47.   id recordList;
  48.   id database;
  49.   id dialog;
  50. }
  51.  
  52. /*====================================================================
  53.                               Initialize
  54. ====================================================================*/
  55.  
  56.  
  57. /*====================================================================
  58.                                  Free
  59. ====================================================================*/
  60.  
  61.  
  62. /*====================================================================
  63.                Methods for access to Instance Variables
  64. ====================================================================*/
  65.  
  66.  
  67. /*====================================================================
  68.                             Public methods
  69. ====================================================================*/
  70.  
  71. /*--------------------------------------------------------------------
  72. |-openFile: sender|
  73.  
  74. Return self.
  75. --------------------------------------------------------------------*/
  76. -openFile: sender
  77. {
  78.   id openDlg = [[FileDlg alloc] initForOpen: "Select database file"
  79.                          withFilter: "*.dbf"];
  80.   unsigned long i;
  81.   char buffer[1024];
  82.   char fname[_MAX_FNAME],ext[_MAX_EXT];
  83.  
  84.   if (([openDlg runModalFor: dialog]) &&
  85.       ([openDlg result] == DID_OK)) {
  86.  
  87.     if (database) {
  88.       [container removeAllRecords: nil];
  89.       [container removeAllColumns: nil];
  90.  
  91.       [[recordList freeObjects] free];
  92.       [database free];
  93.  
  94.       recordList = nil;
  95.     }
  96.  
  97.     database = [[DBFile alloc] init: [openDlg fileName]];
  98.  
  99.     if (!database) {
  100.       WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  101.              "File not found!","Database viewer",0,MB_OK | MB_WARNING);
  102.       [openDlg free];
  103.       [dialog setTitle: "Database Viewer - no file"];
  104.       return nil;
  105.     }
  106.  
  107.     [container detailView: sender];
  108.  
  109.     for (i = 0;i < [database fieldCount];i++) {
  110.       [container addColumn: (char *) [[database field: i] propertyName]];
  111.       [container setColumnTitleAttributes: [container columnTitleAttributes]
  112.        | CFA_FITITLEREADONLY];
  113.       [container setColumnDataAttributes: [container columnDataAttributes]
  114.        | CFA_FIREADONLY];
  115.     }
  116.  
  117.     recordList = [[DBList alloc] initForEntity: database];
  118.  
  119.     [recordList fetchAllRecords: nil];
  120.  
  121.     [container insertObjectList: recordList];
  122.  
  123.     [container invalidate];
  124.  
  125.     _splitpath ([openDlg fileName],NULL,NULL,fname,ext);
  126.  
  127.     sprintf (buffer,"Database Viewer - %s%s",fname,ext);
  128.     [dialog setText: buffer];
  129.   }
  130.  
  131.   [openDlg free];
  132.   return self;
  133. }
  134.  
  135.  
  136. /*====================================================================
  137.                            Private methods
  138. ====================================================================*/
  139. /*--------------------------------------------------------------------
  140. |-awakeFromInterfaceFile| 
  141.  
  142. Return self.
  143.                                                           /1995-Oct-15
  144. --------------------------------------------------------------------*/
  145. -awakeFromInterfaceFile
  146. {
  147.   [container setSize: 0:0:[dialog width]:[dialog height]];
  148.   [dialog bindCommand: 2000 withObject: self selector: @selector(openFile:)];
  149.   return self;
  150. }
  151.  
  152.  
  153. /*--------------------------------------------------------------------
  154. |-parse: (char **) argv count: (int) argc| 
  155.  
  156. Return self.
  157.                                                           /1995-Oct-24
  158. --------------------------------------------------------------------*/
  159. -parse: (char **) argv count: (int) argc
  160. {
  161.   int i,j;
  162.  
  163.   for (i = 1;i < argc;i++)
  164.     if (fileExists (argv[i])) {
  165.       char buffer[1024];
  166.       char fname[_MAX_FNAME],ext[_MAX_EXT];
  167.  
  168.       database = [[DBFile alloc] init: argv[i]];
  169.  
  170.       if (!database) {
  171.     WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  172.                "File not found!","Database viewer",0,
  173.                MB_OK | MB_WARNING);
  174.     [dialog setTitle: "Database Viewer - no file"];
  175.     return nil;
  176.       }
  177.  
  178.       [container detailView: dialog];
  179.  
  180.       for (j = 0;j < [database fieldCount];j++) {
  181.     [container addColumn: (char *) [[database field: j] propertyName]];
  182.     [container setColumnTitleAttributes: [container columnTitleAttributes]
  183.      | CFA_FITITLEREADONLY];
  184.     [container setColumnDataAttributes: [container columnDataAttributes]
  185.      | CFA_FIREADONLY];
  186.       }
  187.  
  188.       recordList = [[DBList alloc] initForEntity: database];
  189.  
  190.       [recordList fetchAllRecords: nil];
  191.  
  192.       [container insertObjectList: recordList];
  193.  
  194.       [container invalidate];
  195.  
  196.       _splitpath (argv[i],NULL,NULL,fname,ext);
  197.  
  198.       sprintf (buffer,"Database Viewer - %s%s",fname,ext);
  199.       [dialog setText: buffer];
  200.       return self;
  201.     }
  202.  
  203.   return self;
  204. }
  205.  
  206.  
  207. /*====================================================================
  208.                           Archiving methods
  209. ====================================================================*/
  210.  
  211.  
  212. @end
  213.